//app.js
var site_address = require('/utils/address.js').address;
App({
  onLaunch: function () {
    this.address()
  },
  onShow: function(){
    this.checkLogin()
  },
  req: function (obj) {
    var that = this
    var method = obj.method || "GET";
    var url = that.globalData.api + obj.url;
    var data = obj.data || {};
    var dataType = obj.dataType || "json";
    var success = obj.success;
    var result = null
    var token = ""
    if (that.globalData.Authorization) {
      token = that.globalData.Authorization
    }
    wx.request({
      url: url,
      method: method,
      dataType: dataType,
      data: data,
      header: {
        Authorization: token
      },
      success: function (res) {
        if (res.statusCode == 403 || res.statusCode == 405) {
          that.checkLogin()
          wx.showToast({
            title: '网络问题，请重试'
          })
        } else {
          success(res)
        }
      },
      fail: function (a, b, c) {
        console.log("fail")
        console.log(a, b, c)
      }
    })
  },
  checkLogin() {
    var that = this
    that.req({
      url: "User/Login",
      method: "POST",
      data: {
        LoginNo: wx.getStorageSync("LoginNo"),
        Password: '1'
      },
      success: function (res) {
        if (res.data.Code) {
          that.globalData.Authorization = res.data.Result
        }
      }
    })
  },
  //  三级联动
  address:function(){
    // console.log(site_address)
    var list1 = []
    var list11 = []
    var list2 = {}
    var list22 = {}
    var list3 = {}
    for (let i = 0; i < site_address.length; i++) {
      if (site_address[i].TopID == 0) {
        list1.push(site_address[i].AddName)
        list11.push(site_address[i].ID)
      }
    }
    this.globalData.city = list1
    for (let i = 0; i < list11.length; i++) {
      var arr = []
      var arr1 = []
      for (let j = 0; j < site_address.length; j++) {
        if (site_address[j].TopID == list11[i]) {
          arr.push(site_address[j].AddName)
          arr1.push(site_address[j].ID)
        }
      }
      list2[list1[i]] = arr
      list22[list1[i]] = arr1
      this.globalData.station= list2
    }
    for (let i in list22) {
      for (let j in list22[i]) {
        var arr2 = []
        for (let k = 0; k < site_address.length; k++) {
          if (site_address[k].TopID == list22[i][j]) {
            arr2.push(site_address[k].AddName)
          }
        }
        list3[list2[i][j]] = arr2
      }
    }
    this.globalData.perList = list3

  },
  globalData: {
    userInfo: null,
    // api: "http://139.129.55.172:92/api/v1/",
    // img: "http://139.129.55.172:92",
    api:'https://apiwx.taihuacn.com/api/v1/',
    img:'https://apiwx.taihuacn.com',
    appId: 'wxb328d5fb3539d716',
    Authorization: null,
    sceret: "14845766956b5da8d0f35d8054e308a1",
   
  },
    

  req: function (obj) {
    var that = this
    var method = obj.method || "GET";
    var url = that.globalData.api + obj.url;
    var data = obj.data || {};
    var dataType = obj.dataType || "json";
    var success = obj.success;
    var result = null
    var token = ''
    if (that.globalData.Authorization) {
      token = that.globalData.Authorization
    }
    wx.request({
      url: url,
      method: method,
      dataType: dataType,
      data: data,
      header: {
        Authorization: token
      },
      success: function (res) {
        if (res.statusCode == 403) {
          that.globalData.Authorization = null,
            that.req({
              url: "Login",
              method: "POST",
              data: {
                LoginNo: wx.getStorageSync("LoginNo"),
                Password: '1'
              },
              success: function (res) {
                if (res.data.Code) {
                  that.globalData.Authorization = res.data.Result
                }
              }
            })
          wx.showToast({
            title: '网络问题，请重试'
          })
        } else {
          success(res)
        }
      },
      fail: function (a, b, c) {
        console.log("fail")
        console.log(a, b, c)
      }
    })
  }
})
